import request from '@/utils/request'
const domainCode = 'marketing'
const appCode = 'slm'
import { setStore, getStore } from '@/utils/store'
/**
     * 根据字典类型查询字典值
     * @param {string} dictTypeCode 数据字典编码
     * @param {string} appCode 应用编码
     * @param {string} domainCode 业务域编码
     * @returns {Array} 数据字典值列表
     * */
export function getDictList({ dictTypeCode }, _appCode, _domainCode) {
  return request({
    url: `/data-dict/api/v2/dictionary/${dictTypeCode}`,
    method: 'get',
    params: { appCode: _appCode || appCode, domainCode: _domainCode || domainCode }
  })
}

/**
 * 根据字典类型查询字典值
 * @param {string} dictTypeCode 数据字典编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictByType = async(dictTypeCode) => {
  const res = await request({
    url: `/data-dict/api/v2/dictionary/${dictTypeCode}`,
    method: 'get',
    params: { appCode, domainCode }
  })
  return res && res.data
}

const flags = {}
/**
 * @param {string} dictTypeCode 数据字典编码
 */
export function SLMDict(dictTypeCode, _domainCode, _appCode) {
  const key = `${dictTypeCode}_${_appCode || appCode}_${_domainCode || domainCode}`
  try {
    const content = getStore({ name: key })
    if (content instanceof Array) {
      return Promise.resolve({
        data: content
      })
    }
  } catch (error) {
    console.log(error)
  }
  if (flags[key] instanceof Array) {
    return new Promise((resolve, reject) => {
      flags[key].push({ resolve, reject })
    })
  }
  flags[key] = []
  return getDictList({ dictTypeCode }, _appCode, _domainCode).then(res => {
    res.data = (res.data || []).sort((a, b) => {
      return a.sort > b.sort
    })
    setStore({
      name: key,
      content: res.data,
      type: 'sessionStorage'
    })
    if (flags[key] instanceof Array) {
      for (const item of flags[key]) {
        item.resolve(res)
      }
    }
    return res
  }).catch(res => {
    if (flags[key] instanceof Array) {
      for (const item of flags[key]) {
        item.reject(res)
      }
    }
    return Promise.reject()
  }).finally(() => {
    delete flags[key]
  })
}

/**
 * 根据字典类型批量查询字典值列表
 * @param {Array} dictTypeCodes 数据字典编码
 * @returns {Array} 数据字典值列表
 * */
export const getDictByPatchType = async(dictTypeCodes) => {
  const res = await request({
    url: `/data-dict/api/v2/dictionary/patch/dictTypeCode`,
    method: 'post',
    params: { appCode, domainCode },
    data: dictTypeCodes
  })
  return res && res.data
}

// 获取省列表
export function getProvinces() {
  return request({
    url: '/select-service/api/v1/administrativeDivisions/provinces',
    method: 'get'
  })
}

// 获取市列表
export function getCities(parentId) {
  return request({
    url: '/select-service/api/v1/administrativeDivisions',
    method: 'get',
    params: { parentId }
  })
}

// 获取区列表
export function getCounties(parentId) {
  return request({
    url: '/select-service/api/v1/administrativeDivisions',
    method: 'get',
    params: { parentId }
  })
}

// 获取街道列表
export function getStreets(parentId) {
  return request({
    url: '/select-service/api/v1/administrativeDivisions',
    method: 'get',
    params: { parentId }
  })
}

/**
 * 获取物流方式
 * */
export const getTransportMode = async() => {
  const res = await request({
    url: `/select-service/api/v1/transportMode/`,
    method: 'get'
  })
  return res && res.data
}

// ===============================maxiaolong
/**
 * 数据字典-------------
 */

// 上样推荐方案
// Series
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function Series(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/SERIES_NAME',
    method: 'get',
    params
  })
}
// 产品定位
// Recommend
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function Recommend(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/RECOMMEND_PROGRAM',
    method: 'get',
    params
  })
}
// 市场层级
// Market
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function Market(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/market_level',
    method: 'get',
    params
  })
}
// 区域类别
// AreaType
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function AreaType(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/AREA_TYPEAREA_TYPE',
    method: 'get',
    params
  })
}
// 款式形态
// StyleForm
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function StyleForm(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/STYLE_FORM',
    method: 'get',
    params
  })
}
// 所属模块
// BELONG_MODEL
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function BelongModel(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/BELONG_MODEL',
    method: 'get',
    params
  })
}
// 附件类型
// ATTACHMENT_TYPE
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function AttachmendType(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/ATTACHMENT_TYPE',
    method: 'get',
    params
  })
}
// 业务类型
// BizType
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function BizType(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/DESIGN_BIZ_TYPE',
    method: 'get',
    params
  })
}
// 门店范围
// DECORATION_RANGE
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function DecorattionRange(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/DECORATION_RANGE',
    method: 'get',
    params
  })
}
// 限时分类
// TIME_LIMIT_CLASS
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function timeLimitClass(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/TIME_LIMIT_CLASS',
    method: 'get',
    params
  })
}

// 验收类型
// ACCEPT_TYPE
// params: pricingTypeCode=appCode=slm&domainCode=marketing
export function AcceptType(params) {
  return request({
    url: '/data-dict/api/v2/dictionary/ACCEPT_TYPE',
    method: 'get',
    params
  })
}

/**
 * 门店
 * */
export const getShop = async(page) => {
  const res = await request({
    url: `/select-service/api/v1/shop/page`,
    method: 'post',
    params: { page: page.page, rows: page.rows, paramsRaw: { name: page.name }}
  })
  return res
}
/**
 * 获取mdm数据
 * @param {*} custDocCode 
 * @returns 
 */
export function getMdmData(custDocCode) {
  const data = new FormData()
  data.append('pageFlag', false)
  const filtersRaw = [
    { id: 'custDocCode', property: 'custDocCode', value: custDocCode, operator: '=' }
  ]
  data.append('filtersRaw', JSON.stringify(filtersRaw))
  return request({
    url: '/select-service/api/v1/custDocDef/page',
    method: 'POST',
    data
  }).then(res => {
    return res.data.records
  })
}
